<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Booking;
use App\Models\User;
use App\Services\Wechat;


class BookingController extends Controller
{
    public function index()
    {   //dd(str_random(60));
    	
    	$where = [];
    	//$where['user_id'] = session('user')->id;    	
    	$tasks = Booking::where($where)->paginate(10);
        return view('4s.booking');
    }

    public function add()
    {   	   	
        return view('4s.booking-add');
    }

    public function save(Request $request)
    {   
    	$rs['status'] = true;
        $data = $request->except(['_token']);
        $wx_user =  session('wechat.oauth_user.default');
        $data['openid'] = $wx_user->id;
    	$flag = Booking::create($data);  
        if($flag) {
            $users = User::all();
            $msg = $flag->toArray();
            foreach ($users as $k => $v) {
                if(empty($v->openid)) continue;
                $msg['openid'] = $v->openid;
                $msg['url'] = config('app.url').'/user/booking-show/'.$flag->id;
                Wechat::sendMsgTpl('booking',$msg);
            }
           
            $rs['id'] = $flag->id;
        } 	
        return back()->with('rs',$rs);
    }

    public function show($id)
    {
        $data = Booking::find($id);
        return view('4s.booking-show',compact('data'));
    }

  
    
}
